﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace LxjCommon.Net
{
    public class IP
    {
        #region 穿过代理服务器获得Ip地址,如果有多个IP，则第一个是用户的真实IP，其余全是代理的IP，用逗号隔开
        public static string GetRealIp()
        {
            string UserIP;
            if (HttpContext.Current.Request.Headers["Cdn-Src-Ip"] != null)
            {
                UserIP = HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
            }
            else if (!string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_VIA"]))  //得到穿过代理服务器的ip地址
            {
                if (!string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
                {
                    UserIP = GetFirstIp(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);
                }
                else
                {
                    UserIP = HttpContext.Current.Request.ServerVariables["HTTP_VIA"];
                }
            }
            else
            {
                UserIP = HttpContext.Current.Request.UserHostAddress;
            }
            return UserIP;
        }
        #endregion

        #region 获取包含代理ip的ip串的第一个IP
        public static string GetFirstIp(string ips)
        {
            if ((ips == null) || (ips.Length <= 0))
            {
                return "";
            }

            string[] ip = ips.Split(',');

            if (ip.Length > 0)
            {
                return ip[0].ToString().Trim();
            }
            else
            {
                return ips;
            }

        }
        #endregion

        #region 把ip转换为整数
        public static long getip(string orignip)
        {
            string[] streachip = orignip.Split('.');
            long intcip = 0;
            for (int i = 0; i < 4; i++)
            {
                intcip += (long)(int.Parse(streachip[i]) * System.Math.Pow(256, 3 - i));
            }
            return intcip;
        }
        #endregion

        #region 把整数转换城IP
        public static string LongToIP(long LongIP)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(LongIP >> 24); sb.Append(".");
                sb.Append(Convert.ToString((LongIP & 0x00FFFFFF) >> 16)); sb.Append(".");
                sb.Append(Convert.ToString((LongIP & 0x0000FFFF) >> 8)); sb.Append(".");
                sb.Append(Convert.ToString(LongIP & 0x000000FF));
                return sb.ToString();
            }
            catch (Exception ex)
            {
                throw ex;
                return ex.ToString();
            }

        }
        #endregion

        #region 检查ip是否正确
        public static bool CheckIsIp(string ip)
        {
            try
            {

                if (ip.Trim().Length <= 0)
                {
                    return true;
                }

                System.Net.IPAddress temp = System.Net.IPAddress.Parse(ip);

                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion
    }
}
